package com.zmcn.flowindex.fw.tool.face;

import java.util.List;

/**
 * 通过接口输出需求代码
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class FaceOut {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String path = "E:\\ksyx\\src\\main\\java\\com\\zmcn\\business\\service\\PadService.java";
		List<FunVO> t = FaceParse.parse(path, "utf-8");
		for (FunVO v : t) {
			System.out.println(FaceOut.toAction(v));
		}

	}

	/**
	 * 输出注释
	 * 
	 * @return
	 */
	public static String toNote(FunVO pvo) {
		StringBuffer sb = new StringBuffer();
		sb.append("	/**\r\n");
		sb.append("	 * " + pvo.getFunName().getDesc() + "\r\n");
		sb.append("	 *\r\n");
		for (FunParaVO vo : pvo.getParaList()) {
			sb.append("	 * @param " + vo.getName() + "\r\n");
			sb.append("	 *            " + vo.getDesc() + "\r\n");
		}
		if (!"void".equals(pvo.getBack().getType())) {
			sb.append("	 * @return " + pvo.getBack().getDesc() + "\r\n");
		}
		sb.append("	 */\r\n");
		return sb.toString();
	}

	/**
	 * 输出带注释的接口
	 * 
	 * @return
	 */
	public static String toFace(FunVO pvo) {
		StringBuffer sb = new StringBuffer();
		sb.append(toNote(pvo));
		String s = "";
		if (pvo.getParaList().size() > 0) {
			for (FunParaVO vo : pvo.getParaList()) {
				s += ", " + vo.getType() + " " + vo.getName();
			}
			s = s.substring(", ".length());
		}
		sb.append("	" + pvo.getBack().getType() + " " + pvo.getFunName().getName() + "(" + s + ");\r\n");
		return sb.toString();
	}

	/**
	 * 输出带注释的接口实现
	 * 
	 * @return
	 */
	public static String toFaceImpl(FunVO pvo) {
		StringBuffer sb = new StringBuffer();
		sb.append(toNote(pvo));
		String s = "";
		if (pvo.getParaList().size() > 0) {
			for (FunParaVO vo : pvo.getParaList()) {
				s += ", " + vo.getType() + " " + vo.getName();
			}
			s = s.substring(", ".length());
		}
		sb.append("	public " + pvo.getBack().getType() + " " + pvo.getFunName().getName() + "(" + s + "){\r\n");
		String t = pvo.getBack().getType();
		if (!"void".equals(t)) {
			if (t.equals("int") || t.equals("float") || t.equals("double") || t.equals("long")) {
				sb.append("		" + pvo.getBack().getType() + " result;\r\n");
			} else if (t.startsWith("List")) {
				sb.append("		" + pvo.getBack().getType() + " result = new Array" + t + "();\r\n");
			} else {
				sb.append("		" + pvo.getBack().getType() + " result=null;\r\n");
			}
			sb.append("		\r\n		return result;\r\n");
		}
		sb.append("	}\r\n");
		return sb.toString();
	}

	/**
	 * 输出action
	 * 
	 * @return
	 */
	public static String toAction(FunVO pvo) {
		StringBuffer sb = new StringBuffer();
		sb.append("	/**\r\n");
		sb.append("	 * " + pvo.getFunName().getDesc() + "Action方法 \r\n");
		sb.append("	 *\r\n");
		sb.append("	 * @return 跳转页面值\r\n");
		sb.append("	 */\r\n");
		sb.append("	@Action(value = \"/" + pvo.getFunName().getName() + "\")\r\n");
		sb.append("	public String " + pvo.getFunName().getName() + "() {\r\n");
		sb.append("		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();\r\n");
		sb.append("		try {\r\n");
		sb.append("			//获取请求参数并调用方法\r\n");
		String ps = "";
		if (pvo.getParaList().size() > 0) {
			sb.append("			HttpServletRequest req = Struts2Utils.getRequest();\r\n");
			for (FunParaVO vo : pvo.getParaList()) {
				sb.append("			" + vo.getType() + " " + vo.getName() + " = ServletRequestUtils.get" + getFB(vo.getType()) + "Parameter(req, \"" + vo.getName() + "\");\r\n");
				ps += ", " + vo.getName();
			}
			ps = ps.substring(", ".length());
		}

		sb.append("			" + pvo.getBack().getType() + " result = service." + pvo.getFunName().getName() + "(" + ps + ");\r\n");
		sb.append("			//合成结果 \r\n");
		sb.append("			resultMap.put(\"code\", CodeUtil.SUCCESS);\r\n");
		sb.append("			resultMap.put(\"info\", result);\r\n");
		if (pvo.getParaList().size() > 0) {
			sb.append("		} catch (ServletRequestBindingException err) {\r\n");
			sb.append("			log.error(err.getMessage(), err);\r\n");
			sb.append("			resultMap.put(\"code\", CodeUtil.PARA_ERR);\r\n");
		}
		sb.append("		} catch (Exception err) {\r\n");
		sb.append("			log.error(err.getMessage(), err);\r\n");
		sb.append("			resultMap.put(\"code\", CodeUtil.UNKOWN_ERR);\r\n");
		sb.append("		}\r\n");
		sb.append("		JsonUtil.outJson(resultMap, getResponse());\r\n");
		sb.append("		return null;\r\n");
		sb.append("	}\r\n");
		return sb.toString();

	}
	
	/**
	 * 输出action
	 * 
	 * @return
	 */
	public static String toAction2(FunVO fvo) {
		StringBuffer sb = new StringBuffer();
		sb.append("	/**\r\n");
		sb.append("	 * " + fvo.getFunName().getDesc() + "Action方法 \r\n");
		sb.append("	 *\r\n");
		sb.append("	 * @return 跳转页面值\r\n");
		sb.append("	 */\r\n");
		sb.append("	@Action(value = \"/" + fvo.getFunName().getName() + "\")\r\n");
		sb.append("	public String " + fvo.getFunName().getName() + "() {\r\n");
		sb.append("		try {\r\n");
		sb.append("			//获取请求参数并调用方法\r\n");
		String ps = "";
		if (fvo.getParaList().size() > 0) {
			sb.append("			HttpServletRequest req = getRequest();\r\n");
			for (FunParaVO vo : fvo.getParaList()) {
				sb.append("			" + vo.getType() + " " + vo.getName() + " = ServletRequestUtils.get" + getFB(vo.getType()) + "Parameter(req, \"" + vo.getName() + "\");\r\n");
				ps += ", " + vo.getName();
			}
			ps = ps.substring(", ".length());
		}

		sb.append("			" + fvo.getBack().getType() + " result = service." + fvo.getFunName().getName() + "(" + ps + ");\r\n");
		sb.append("			//结果处理 \r\n");
		if (fvo.getParaList().size() > 0) {
			sb.append("		} catch (ServletRequestBindingException err) {\r\n");
			sb.append("			log.error(err.getMessage(), err);\r\n");
		}
		sb.append("		} catch (Exception err) {\r\n");
		sb.append("			log.error(err.getMessage(), err);\r\n");
		sb.append("		}\r\n");
		sb.append("		return \"\";\r\n");
		sb.append("	}\r\n");
		return sb.toString();

	}

	private static String getFB(String s) {
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
}
